package wxdb.parse.absyn;

import java.util.*;

import wxdb.metadata.TableMgr;
import wxdb.query.*;
import wxdb.record.*;
import wxdb.ui.Global;

public class QryExp extends Exp{
	public FldsExp flsExp;
	public FromLst frmLst;
	public WhereExp whereExp;
	public FldsExp grpFlsExp;
	public FldsExp ordFlsExp;
	public HavingExp he;
	
	private HashMap<String,ArrayList<String>> fields = new HashMap<String,ArrayList<String>>();;
	private ArrayList<String> tables = new ArrayList<String>();
	private HashMap<String,Predicate> predtwo = new HashMap<String,Predicate>();
	private Predicate pred;
	
	public QryExp(int p, FldsExp fe, FromLst fl, WhereExp we, FldsExp ge, HavingExp he, FldsExp ob){
		pos = p;
		flsExp = fe;
		frmLst = fl;
		whereExp = we;
		grpFlsExp = ge;
		ordFlsExp = ob;
		this.he = he;
		
		if(ge!=null){
			Global.GROUP_BY = true;
			Global.group_by_field = ((NameFldExp)ge.fldExp).getName();
		}
		if(ob!=null){
			Global.ORDER_BY = true;
			Global.order_by_field = ((NameFldExp)ob.fldExp).getName();
		}
		
		tables = fl.tables();

		if(fe!=null)
			fields = fe.getFileds(tables.get(0));
		else{
			for(String tblname : tables){
				fields.put(tblname, new ArrayList<String>());
				TableInfo ti = TableMgr.getMeta(Global.currentdb, tblname);
				Schema sch = ti.getSchema();
				Set<String> s = sch.getFields();
				for(String field : s){			
					fields.get(tblname).add(field);
				}
			}
		}
		pred = new Predicate();
		if(we!=null)
			pred = we.whereList().predicate();
	}
	
	public ArrayList<String> tables(){
		return tables;
	}
	
	public HashMap<String,ArrayList<String>> fields(){
		return fields;
	}
	
	public Predicate pred(){
		return pred;
	}
}
